from datetime import datetime

import requests
from bs4 import BeautifulSoup


def bin_total():
    cookies = {
        '_ga': 'GA1.1.479296914.1720401671',
        '_gcl_au': '1.1.1032536040.1720401688',
        '_clck': 'twbqhy%7C2%7Cfnb%7C0%7C1650',
        '__gads': 'ID=964377270d81dfdc:T=1720401670:RT=1720491412:S=ALNI_MafsA0w8NgPE7tBkjYnl9pmhj3pYA',
        '__gpi': 'UID=00000e8604216933:T=1720401670:RT=1720491412:S=ALNI_Mb53FjfZiuHahihoIjmI79dZukVCg',
        '__eoi': 'ID=1b11dd7476c0dc48:T=1720401670:RT=1720491412:S=AA-AfjZZvZ1USD1vHMtaccsoi2cV',
        'cfzs_google-analytics_v4': '%7B%22OFGW_pageviewCounter%22%3A%7B%22v%22%3A%2233%22%7D%7D',
        'XSRF-TOKEN': 'eyJpdiI6Ii95dHc3MTBsU3oraGY3RWdvOWlEQ3c9PSIsInZhbHVlIjoiYmR6d3B2Sk5sNW13NjRvWkdHVnc0U1o4M3NLTitnSkx3Z2hOcDFpVFpqMzlKcCtBNEs4djRoM2VrRXJRclh2ckUwYk81cGFWUHZtZk5kVk1QelBvdU9TNzhRcUhxYjhMU0NDeldFbmJQSENTZm1UdGpTcUM5UVd2bS9ZcXdYQjYiLCJtYWMiOiJhODg3MTgyMGU1YjJkZTlkZWI4NjFmY2VkMTliZTExYjE5NWFmY2YxNDE0NTM3MmE4OTk0MjMxMDRhMWU2ODI0In0%3D',
        'bincheck_session': 'eyJpdiI6InNuYzI4Szh4Z0FkMnpEeVA5NU1nckE9PSIsInZhbHVlIjoiRUY5R0pjNHJOR3p2MVZZd0x4cVhuejlEVXhxREdqM1VCRS9LYUJSUmNya3ZydFhjOTY0Z2IvQVBSYW0xN0h3VmIvcytZZURMTFIrcHV4OGZSdUhORGJNOWhBR1pROVgrY0p5VCtCL3VjUFM0Mnh3WWtlUk96YkhsQ29kU2V4NkoiLCJtYWMiOiJlY2Q0YjZjMWMwNGMzZWQ1NDM4YjdlODdkMDMwYmRkNjBlYTMyNWQxZDFiNGI2NTY1NWFjODllM2I2MWY1OGJjIn0%3D',
        '_clsk': '15cfe1j%7C1720491427045%7C3%7C1%7Cb.clarity.ms%2Fcollect',
        '_ga_CGDE2TMKTW': 'GS1.1.1720491413.4.1.1720491427.46.0.0',
        'FCCDCF': '%5Bnull%2Cnull%2Cnull%2Cnull%2Cnull%2Cnull%2C%5B%5B13%2C%22%5B%5C%22DBABBg~BUoAAACA%5C%22%2C%5B'
                  '%5B8%2C%5B1720491424%2C552133000%5D%5D%5D%5D%22%5D%5D%5D',
        'FCNEC': '%5B%5B%22AKsRol-v9Tp-Lc1dpKOtgz'
                 '-g2jvXFGvZfDPOYxRnmdYzjPTl2MjZTPPQfpIy9zRqmdXsElI0t9pY8BXBUARLbqUPfJyddGt2lpgtXzOle5'
                 'kcd8lQJgLHi6gKwSXjkymwbw7rPVqZKk2jdp0613YG_DSqzB-V6WVIoQ%3D%3D%22%5D%5D',
        'cfz_google-analytics_v4': '%7B%22OFGW_engagementDuration%22%3A%7B%22v%22%3A%222536%22%2C%22e%22'
                                   '%3A1752027425564%7D%2C%22OFGW_engagementStart%22%3A%7B%22v%22%3A1720491470423%2C'
                                   '%22e%22%3A1752027468236%7D%2C%22OFGW_counter%22%3A%7B%22v%22%3A%2279%22%2C%22e%22'
                                   '%3A1752027423028%7D%2C%22OFGW_session_counter%22%3A%7B%22v%22%3A%228%22%2C%22e%22'
                                   '%3A1752027423028%7D%2C%22OFGW_ga4%22%3A%7B%22v%22%3A%22fba09baf-bce5-4702-93ec'
                                   '-e6ba66292421%22%2C%22e%22%3A1752027423028%7D%2C%22OFGW__z_ga_audiences%22%3A%7B'
                                   '%22v%22%3A%22fba09baf-bce5-4702-93ec-e6ba66292421%22%2C%22e%22%3A1751937668785%7D'
                                   '%2C%22OFGW_let%22%3A%7B%22v%22%3A%221720491423028%22%2C%22e%22%3A1752027423028%7D'
                                   '%2C%22OFGW_ga4sid%22%3A%7B%22v%22%3A%221302034812%22%2C%22e%22%3A1720493223028%7D'
                                   '%7D',
    }

    headers = {
        'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,'
                  'application/signed-exchange;v=b3;q=0.7',
        'accept-language': 'en-US,en;q=0.9',
        'cache-control': 'max-age=0',
        'priority': 'u=0, i',
        'referer': 'https://bincheck.io/bin-list',
        'sec-ch-ua': '"Not/A)Brand";v="8", "Chromium";v="126", "Google Chrome";v="126"',
        'sec-ch-ua-mobile': '?0',
        'sec-ch-ua-platform': '"Windows"',
        'sec-fetch-dest': 'document',
        'sec-fetch-mode': 'navigate',
        'sec-fetch-site': 'same-origin',
        'sec-fetch-user': '?1',
        'upgrade-insecure-requests': '1',
        'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) '
                      'Chrome/126.0.0.0 Safari/537.36',
    }

    response = requests.get('https://bincheck.io/bin-list', cookies=cookies, headers=headers)
    if response.status_code == 200:
        return response.text
    else:
        return None


def html_analysis(html):
    """
    解析单个HTML,得到数据
    :param html:
    :return:
    """
    soup = BeautifulSoup(html, 'html.parser')
    a_items = (
        soup.find_all("a", class_="flex items-start inline-block gap-3 p-5 space-x-4 transition ease-in-out border "
                                    "rounded-md border-slate-200 duraton-150 hover:border hover:border-blue-400 "
                                    "dark:border-slate-500 dark:hover:border-blue-400 country")
    )
    result_list = []
    for item in a_items:
        href = item.get("href")
        li_list = item.find_all('li')
        country = item.find("h3")
        detail = item.find("p")
        if country is None or detail is None:
            continue
        json_item = {
            "country": country.text,
            "detail": detail.text,
            "bin_numbers": li_list[0].text.split("Total of BIN numbers: ")[-1],
            "visa": li_list[1].text.split("Visa: ")[-1],
            "amex": li_list[2].text.split("Amex: ")[-1],
            "master_card": li_list[3].text.split("MasterCard: ")[-1],
            "discover": li_list[4].text.split("Discover: ")[-1],
            "other_brands": li_list[5].text.split("Other brands: ")[-1],
            "country_abbr": href.split("/")[-1],
            "create_time": datetime.now().strftime('%Y-%m-%d %H:%M:%S'),
            "update_time": datetime.now().strftime('%Y-%m-%d %H:%M:%S'),
        }
        result_list.append(tuple(json_item.values()))
    return result_list


def get_country_total():
    return html_analysis(bin_total())


if __name__ == '__main__':
    result = bin_total()
    result_obj = html_analysis(result)
    print(len(result_obj))
    print(result_obj)
